//
//  Bootloader.m
//  sweetbox
//
//  Created by apollo on 15/9/4.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//



#import <Foundation/Foundation.h>

#import <EAIntroView/EAIntroPage.h>
#import <EAIntroView/EAIntroView.h>
#import <FMDB/FMDB.h>

#import "Bootloader.h"
#import "log.h"
#import "DBHelper.h"

@interface Bootloader ()
@property (nonatomic) UITabBarController *tarbarVC;

- (void) buildEAIntroView;

@end

@implementation Bootloader


+ (Bootloader *) sharedBootloader {
    static dispatch_once_t pred = 0;
    __strong static Bootloader *_sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] initWithTabBar];
        
    });
    return _sharedObject;
}

- (Bootloader *) initWithTabBar {
    if (self = [super init]) {
        self.tarbarVC = [[UITabBarController alloc] init];
        self.rootViewController = self.tarbarVC;
    }
    return self;
}

- (void) setVCs:(NSArray *) vcs {
    if (nil == vcs) {
        //TODO: Add Log
        LOG_E("setVCs with nullptr");
        return ;
    }
    self.tarbarVC.viewControllers = vcs;
    
    if (vcs.count >=1) {
        self.tarbarVC.selectedIndex  = 0; // show who first
    }
}

- (void) boot {
    
    NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
    BOOL isInstalled = NO;
    isInstalled = [sud boolForKey:@"is_installed"];
    
    if (isInstalled) {
        LOG_I("App Has Already Installed!");
        NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
        NSString *openID = [sud valueForKey:@"openID"];
        NSString *platform = [sud valueForKey:@"platform"];
        BOOL ret = [[DBHelper sharedDBHelper] openDictDB:platform openID:openID];
        if (YES != ret) {
            LOG_E("[[DBHelper sharedDBHelper] openDictDB:platform openID:openID]; Error");
        }
        
        ret = [[DBHelper sharedDBHelper] openPresDB:platform openID:openID];
        if (YES != ret) {
            LOG_E("[[DBHelper sharedDBHelper] openPresDB:platform openID:openID]; Error");
        }

        return ;
    }
    
    [self installApp];
    [self showInterView];
}

- (void) showInterView {
    [self buildEAIntroView];
}

- (void) installApp {
    NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
    [sud setBool:YES forKey:@"is_installed"];
    
    [sud setValue:@"none" forKey:@"platform"];
    NSString *openID =[NSString stringWithFormat:@"%u",arc4random()];
    [sud setValue:openID forKey:@"openID"];
    [[DBHelper sharedDBHelper] createDictDB:@"none" openID:openID];
    [[DBHelper sharedDBHelper] createPresDB:@"none" openID:openID];
}

- (void) buildEAIntroView {
    EAIntroPage *page1 = [EAIntroPage page];
    page1.title = @"方剂手册";
    page1.desc = @"老师说的方剂、书上看到的方剂、同学念叨的方剂，剂剂入册";
    page1.bgImage = [UIImage imageNamed:@"bg2"];
    
    EAIntroPage *page2 = [EAIntroPage page];
    page2.title = @"字典单词";
    page2.desc = @"日常用语、专业单词、四六级词汇，词词入袋";
    page2.bgImage = [UIImage imageNamed:@"bg2"];
    
    EAIntroPage *page3 = [EAIntroPage page];
    page3.title = @"速度开启征程吧！！！";
    page3.bgImage = [UIImage imageNamed:@"bg2"];
    
    UIView *view = self.rootViewController.view;
    EAIntroView *intro = [[EAIntroView alloc] initWithFrame:view.bounds andPages:@[page1,page2,page3]];
    [intro.skipButton setTitle:@"直接跳过" forState: UIControlStateNormal];
    [intro showInView:view animateDuration:0.0];
}

@end
